package com.pengpeng.sheepfoldmall.product.controller;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

import com.pengpeng.sheepfoldmall.product.entity.AttrEntity;
import com.pengpeng.sheepfoldmall.product.service.AttrAttrgroupRelationService;
import com.pengpeng.sheepfoldmall.product.service.AttrService;
import com.pengpeng.sheepfoldmall.product.service.CategoryService;
import com.pengpeng.sheepfoldmall.product.vo.AttrAttrRelationVo;
import com.pengpeng.sheepfoldmall.product.vo.AttrGroupWithAttrsVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.pengpeng.sheepfoldmall.product.entity.AttrGroupEntity;
import com.pengpeng.sheepfoldmall.product.service.AttrGroupService;
import com.pengpeng.common.utils.PageUtils;
import com.pengpeng.common.utils.R;



/**
 * 属性分组
 *
 * @author pengpeng
 * @email pengpeng@gmail.com
 * @date 2023-08-30 16:23:56
 */
@RestController
@RequestMapping("product/attrgroup")
public class AttrGroupController {
    @Autowired
    private AttrGroupService attrGroupService;

    @Autowired
    private CategoryService categoryService;

    @Autowired
    private AttrService attrService;

    @Autowired
    private AttrAttrgroupRelationService attrAttrgroupRelationService;


    /**
     * 查询对应分类下的属性分组以及属性分组下的属性详情（发布商品设置基本参数时回显）
     * @return
     */
    @GetMapping("/{catelogId}/withattr")
    public R getAttrGroupWithAttrs(@PathVariable("catelogId") Long catelogId){
        List<AttrGroupWithAttrsVo> vos = attrGroupService.getAttrGroupWithAttrsByCatelogId(catelogId);
        return R.ok().put("data", vos);
    }
    /**
     * 新增属性与属性分组的关联关系（属性分组关联属性确认时）
     * @return
     */
    @PostMapping("/attr/relation")
    public R addRelation(@RequestBody AttrAttrRelationVo[] vos){
        attrAttrgroupRelationService.saveRelation(vos);
        return R.ok();
    }
    /**
     * 获取属性分组下没有被其他分组关联的的所有基本属性（且只能是本分类下的属性）,属性分组管理点击关联按钮中新增关联接口
     * @param attrGroupId
     * @return
     */
    @GetMapping("/{attrgroupId}/noattr/relation")
    public R attrNoRelation(@PathVariable("attrgroupId") Long attrGroupId,@RequestParam Map<String, Object> params){
        PageUtils page = attrService.getAttrNoRelation(attrGroupId,params);
        return R.ok().put("page",page);
    }
    /**
     * 移除属性与属性分组的关联关系(可能是批量删除,所以参数是一个数组)
     * @return
     */
    @PostMapping("/attr/relation/delete")
    public R deleteRelation(@RequestBody AttrAttrRelationVo[] vos){
        attrGroupService.deleteRelation(vos);
        return R.ok();
    }
    /**
     * 获取属性分组下的所有基本属性（用于属性分组关联回显）
     * @param attrGroupId
     * @return
     */
    @GetMapping("/{attrgroupId}/attr/relation")
    public R attrRelation(@PathVariable("attrgroupId") Long attrGroupId){
        List<AttrEntity> attrEntities = attrService.getAttrRelation(attrGroupId);
        return R.ok().put("data",attrEntities);
    }

    /**
     * 列表（分页查询属性分组）
     */
    @RequestMapping("/list/{catelogId}")
    //@RequiresPermissions("product:attrgroup:list")
    public R list(@RequestParam Map<String, Object> params,@PathVariable("catelogId") Long catelogId){
//        PageUtils page = attrGroupService.queryPage(params);
        PageUtils page = attrGroupService.queryPage(params,catelogId);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{attrGroupId}")
    //@RequiresPermissions("product:attrgroup:info")
    public R info(@PathVariable("attrGroupId") Long attrGroupId){
        //先根据查询出属性分组id查询出属性分组对象
		AttrGroupEntity attrGroup = attrGroupService.getById(attrGroupId);
        //递归查询出属性分组所属分类的完整路径
		Long[] path = categoryService.findCatelogPath(attrGroup.getCatelogId());
		//设置所属分类的完整路径
		attrGroup.setCatelogPath(path);

        return R.ok().put("attrGroup", attrGroup);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    //@RequiresPermissions("product:attrgroup:save")
    public R save(@RequestBody AttrGroupEntity attrGroup){
		attrGroupService.save(attrGroup);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    //@RequiresPermissions("product:attrgroup:update")
    public R update(@RequestBody AttrGroupEntity attrGroup){
		attrGroupService.updateById(attrGroup);

        return R.ok();
    }

    /**
     * 删除分组，并级联删除分组关联的属性关系
     */
    @RequestMapping("/delete")
    //@RequiresPermissions("product:attrgroup:delete")
    public R delete(@RequestBody Long[] attrGroupIds){
		attrGroupService.removeGroupAndRelation(Arrays.asList(attrGroupIds));
        return R.ok();
    }

}
